package find.chat.test;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Instrumentation;
import android.app.Instrumentation.ActivityMonitor;
import android.content.Intent;
import android.os.Handler;
import android.test.suitebuilder.annotation.SmallTest;
import find.chat.*;
import find.chat.R;
import find.chat.tools.FriendController;
import find.chat.types.FriendInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;
import android.test.TouchUtils;
import android.test.ActivityInstrumentationTestCase2;

public class messaging extends ActivityInstrumentationTestCase2<Messaging> {
	private Messaging mActivity;
	private Button sendMessageButton;
	private EditText messageText;

	public messaging() {
		super(Messaging.class);
	}

	protected void setUp() throws Exception {
		super.setUp();

		FriendInfo[] friends = FriendController.getFriendsInfo();

		Intent addFriendsInfo = new Intent();
		addFriendsInfo.putExtra(FriendInfo.USERNAME, friends[0].userName);
		addFriendsInfo.putExtra(FriendInfo.PORT, friends[0].port);
		addFriendsInfo.putExtra(FriendInfo.IP, friends[0].ip);
		setActivityIntent(addFriendsInfo);

		mActivity = (Messaging) getActivity();
		sendMessageButton = (Button) (Button) mActivity
				.findViewById(R.id.sendMessageButton);
		messageText = (EditText) mActivity.findViewById(R.id.message);

		Thread loginThread = new Thread() {
			@Override
			public void run() {
				String result = null;
				try {
					result = mActivity.imService.authenticateUser("chung",
							"12345");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
		};
		loginThread.start();
	}

	@SmallTest
	public void testView() { // checks if the activity is created
		assertNotNull(getActivity());
	}

	public void testCheckLocation() {
		ActivityMonitor am = getInstrumentation().addMonitor(
				GPSActivity.class.getName(), null, false);

		// Click the menu option
		getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
		getInstrumentation().invokeMenuActionSync(mActivity,
				mActivity.CheckLocation, 0);

		Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
		assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
		a.finish();
	}

	public void testRateUserShowing() {
		getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
		getInstrumentation().invokeMenuActionSync(mActivity,
				mActivity.RateUser, 0);
		assertTrue(mActivity.alertDialog.isShowing());
	}

	public void testSendMessage() {
		TouchUtils.tapView(this, messageText);
		sendKeys("T E S T");
		assertEquals("test", messageText.getText().toString());
		// TouchUtils.clickView(this, sendMessageButton);
		sendKeys(66);
	}

	public void testSendMessageEmpty() {
		assertEquals("", messageText.getText().toString());
		// TouchUtils.clickView(this, sendMessageButton);
		sendKeys(66);
	}

	public void testReceiveMsgMoreThan15() {
		ActivityMonitor am = getInstrumentation().addMonitor(
				Messaging.class.getName(), null, false);
		try {
			mActivity.imService.sendMessage("fchung", "chung",
					"This is a message with length>15");
		} catch (UnsupportedEncodingException e) {
		}
		;

		Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 10000);
		assertTrue(true);
	}

	public void testReceiveMsgLessThan15() {
		ActivityMonitor am = getInstrumentation().addMonitor(
				Messaging.class.getName(), null, false);
		try {
			mActivity.imService.sendMessage("fchung", "chung", "1");
		} catch (UnsupportedEncodingException e) {
		}
		;
		Activity a = getInstrumentation().waitForMonitorWithTimeout(am, 10000);
		assertTrue(true);
	}
}
